﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using WebReference;

public partial class Admin_Yield : System.Web.UI.Page
{
    Service objService = new Service();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack)
        {
        }
        else
        {
            string ExperimentId = (string)Session["ExperimentId"];

            if (ExperimentId != null)
            {
                lblExpdetails.Text = "Your Experiment Id is - " + ExperimentId;
            }

            string id = Request.QueryString["id"];

            if (id != null)
            {
                fillYieldDetails(Convert.ToInt32(id));
                txtYieldId.Text = string.Empty;
                trYieldDg.Visible = false;
                trYieldTextBox.Visible = true;
            }

            bindDataGrid();
        }
    }
    private void bindDataGrid()
    {
        string ExpId = (string)Session["ExperimentId"];

        if (ExpId != null)
        {
            if (objService.GetYield(ExpId).Tables[0].Rows.Count > 0)
            {
                dgYield.DataSource = objService.GetYield(ExpId);
                dgYield.DataBind();
                trYieldDg.Visible = true;
                trYieldTextBox.Visible = false;
            }
            else
            {
                trYieldDg.Visible = false;
                trYieldTextBox.Visible = true;
            }
        }
        else
        {
            lblMessage.Text = "You haven't entered any records for this table";
            trYieldDg.Visible = false;
            trYieldTextBox.Visible = true;
        }
    }
    
    protected void dgYield_RowEditing(object sender, GridViewEditEventArgs e)
    {
        int id = Convert.ToInt32(dgYield.Rows[e.NewEditIndex].Cells[1].Text.Trim());
        fillYieldDetails(id);
        trYieldDg.Visible = false;
        trYieldTextBox.Visible = true;
    }

    private void fillYieldDetails(Int32 id)
    {
        DataSet ds = new DataSet();
        ds = objService.GetTableDetailsBasedonPK("yields", id);
        txtYieldId.Text = ds.Tables[0].Rows[0]["id"].ToString();
        txtGas.Text = ds.Tables[0].Rows[0]["gas"].ToString();
        txtChr.Text = ds.Tables[0].Rows[0]["chr"].ToString();
        txtLiquid.Text = ds.Tables[0].Rows[0]["liquid"].ToString();
        txtTar.Text = ds.Tables[0].Rows[0]["tar"].ToString();
               
    }

    private void clearTextBoxes()
    {
         txtYieldId.Text = string.Empty ;
        txtGas.Text = string.Empty ;
        txtChr.Text = string.Empty ;
        txtLiquid.Text = string.Empty ;
        txtTar.Text = string.Empty;
    }

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string ReferenceID = (string)Session["ReferenceId"];

        if (txtYieldId.Text != string.Empty)
        {
            if (txtGas.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("yields", "gas", "null", txtYieldId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("yields", "gas", txtGas.Text, txtYieldId.Text);
            }
            if (txtChr.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("yields", "chr", "null" , txtYieldId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("yields", "chr", txtChr.Text, txtYieldId.Text);
            }
            if (txtLiquid.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("yields", "liquid", "null", txtYieldId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("yields", "liquid", txtLiquid.Text, txtYieldId.Text);
            }

           
            if (txtTar.Text == string.Empty)
            {
                lblMessage.Text = objService.UpdateTable("yields", "tar", "null", txtYieldId.Text);
            }
            else
            {
                lblMessage.Text = objService.UpdateTable("yields", "tar", txtTar.Text, txtYieldId.Text);
            }

            clearTextBoxes();

            bindDataGrid();

        }

        else
        {
            if ((string)Session["YieldId"] != null)
            {
                string YieldId = (string)Session["YieldId"];
                lblMessage.Text = objService.UpdateYield("gas", txtGas.Text, YieldId);
                lblMessage.Text = objService.UpdateYield("chr", txtChr.Text, YieldId);
                lblMessage.Text = objService.UpdateYield("liquid", txtLiquid.Text, YieldId);
                lblMessage.Text = objService.UpdateYield("tar", txtTar.Text, YieldId);
                lblMessage.Text = objService.UpdateYield("ref_fk", ReferenceID, YieldId);
                bindDataGrid();
                clearTextBoxes();

            }
            else
            {

                if ((string)Session["ExperimentId"] != null)
                {
                    string ExpID = (string)Session["ExperimentId"];
                    lblMessage.Text = objService.SaveYield(txtGas.Text, null, txtChr.Text, null, txtLiquid.Text, null, txtTar.Text, ReferenceID,ExpID);
                    Int32 Yid = objService.GetYieldRecentId(ExpID);
                    Session["YieldId"] = Yid.ToString();
                    lblMessage.Text = "Yield Saved and " + objService.UpdateExperiment("yields_fk", Yid.ToString(), ExpID);
                    bindDataGrid();
                    clearTextBoxes();
                }
                else
                {
                    string RefeId = (string)Session["ReferenceId"];

                    lblMessage.Text = objService.SaveExperiment(null, null, null, null, null, null, null, null, null, null, null, RefeId, null);

                    //string ExperimentId = objService.GetLastInsertId().ToString();
                    string ExperimentId = objService.GetExperimentId("ref_fk", RefeId);
                                       
                    Session["ExperimentId"] = ExperimentId;

                    lblMessage.Text = objService.SaveYield(txtGas.Text, null, txtChr.Text, null, txtLiquid.Text, null,txtTar.Text, ReferenceID, ExperimentId);

                    Int32  YieldId = objService.GetYieldRecentId(ExperimentId);

                    lblMessage.Text = "Yield Saved and " + objService.UpdateExperiment("yields_fk", YieldId.ToString(), ExperimentId);

                    Session["YieldId"] = YieldId.ToString();

                    bindDataGrid();

                    clearTextBoxes();
                }
            }
        }
    }


    protected void dgYield_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgYield.PageIndex = e.NewPageIndex;
        bindDataGrid();
    }
    protected void btnPrevious_Click(object sender, EventArgs e)
    {
        Response.Redirect("Previous Pages/PreYield.aspx");
    }
}
